home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_UDELxntp.idb / usr / freeware / src / xntp-3.4o-export / lib / atolfp.c.z / atolfp.c
C/C++ Source or Header  |  1998-01-21  |  2KB  |  118 lines

  1. /*
  2.  * atolfp - convert an ascii string to an l_fp number
  3.  */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6.  
  7. #include "ntp_fp.h"
  8. #include "ntp_string.h"
  9.  
  10. /*
  11.  * Powers of 10
  12.  */
  13. static u_long ten_to_the_n[10] = {
  14.            0,
  15.           10,
  16.          100,
  17.         1000,
  18.            10000,
  19.           100000,
  20.          1000000,
  21.         10000000,
  22.        100000000,
  23.       1000000000,
  24. };
  25.  
  26.  
  27. int
  28. atolfp(str, lfp)
  29.     const char *str;
  30.     l_fp *lfp;
  31. {
  32.     register const char *cp;
  33.     register u_long dec_i;
  34.     register u_long dec_f;
  35.     char *ind;
  36.     int ndec;
  37.     int isneg;
  38.     static char *digits = "0123456789";
  39.  
  40.     isneg = 0;
  41.     dec_i = dec_f = 0;
  42.     ndec = 0;
  43.     cp = str;
  44.  
  45.     /*
  46.      * We understand numbers of the form:
  47.      *
  48.      * [spaces][-|+][digits][.][digits][spaces|\n|\0]
  49.      */
  50.     while (isspace(*cp))
  51.         cp++;
  52.     
  53.     if (*cp == '-') {
  54.         cp++;
  55.         isneg = 1;
  56.     }
  57.     
  58.     if (*cp == '+')
  59.         cp++;
  60.  
  61.     if (*cp != '.' && !isdigit(*cp))
  62.         return 0;
  63.  
  64.     while (*cp != '\0' && (ind = strchr(digits, *cp)) != NULL) {
  65.         dec_i = (dec_i << 3) + (dec_i << 1);    /* multiply by 10 */
  66.         dec_i += (ind - digits);
  67.         cp++;
  68.     }
  69.  
  70.     if (*cp != '\0' && !isspace(*cp)) {
  71.         if (*cp++ != '.')
  72.             return 0;
  73.     
  74.         while (ndec < 9 && *cp != '\0'
  75.             && (ind = strchr(digits, *cp)) != NULL) {
  76.             ndec++;
  77.             dec_f = (dec_f << 3) + (dec_f << 1);    /* *10 */
  78.             dec_f += (ind - digits);
  79.             cp++;
  80.         }
  81.  
  82.         while (isdigit(*cp))
  83.             cp++;
  84.         
  85.         if (*cp != '\0' && !isspace(*cp))
  86.             return 0;
  87.     }
  88.  
  89.     if (ndec > 0) {
  90.         register u_long tmp;
  91.         register u_long bit;
  92.         register u_long ten_fact;
  93.  
  94.         ten_fact = ten_to_the_n[ndec];
  95.  
  96.         tmp = 0;
  97.         bit = 0x80000000;
  98.         while (bit != 0) {
  99.             dec_f <<= 1;
  100.             if (dec_f >= ten_fact) {
  101.                 tmp |= bit;
  102.                 dec_f -= ten_fact;
  103.             }
  104.             bit >>= 1;
  105.         }
  106.         if ((dec_f << 1) > ten_fact)
  107.             tmp++;
  108.         dec_f = tmp;
  109.     }
  110.  
  111.     if (isneg)
  112.         M_NEG(dec_i, dec_f);
  113.     
  114.     lfp->l_ui = dec_i;
  115.     lfp->l_uf = dec_f;
  116.     return 1;
  117. }
  118.